package com.zy.controller;


import com.zy.entity.User;
import com.zy.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;
    @GetMapping("/test2")
    public String test()
    {
        System.out.println("test");
        return "test";
    }

    @GetMapping("/login")
    public String login(@RequestParam("userName") String userName, @RequestParam("password") String password) {
        System.out.println("userName = " + userName);
        System.out.println("password = " + password);
         User user = userService.login(userName, password);
         if (user != null) {
             System.out.println(user.getId());
             System.out.println(user.getUserName());
             System.out.println(user.getPassword());
             System.out.println(user.getUserType());
             if (user.getUserType() == 0) {
                 return "redirect:/admin.jsp";
             } else {
                 return "redirect:/index.jsp";
             }
         } else {
             return "redirect:/error.jsp";
         }
    }

    @PostMapping("/register")
    public String register(@RequestParam("username") String username,
                           @RequestParam("password") String password) {
        boolean isRegistered = userService.register(username, password);
        if (isRegistered) {
            return "redirect:/index.jsp";
        } else {
            return "redirect:/error.jsp";
        }
    }
}